// Copyright (C) 2010  Joan Puigcerver Perez, <joapuipe@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include "SAXPeopleHandler.hpp"

#include <iostream>

void SAXPeopleHandler::startDocument ( )
{
  _kname = XMLString::transcode("name");
}

void SAXPeopleHandler::endDocument ( )
{
  XMLString::release(&_kname);
}

void SAXPeopleHandler::startElement ( const XMLCh* const uri,
				      const XMLCh* const localname,
				      const XMLCh* const qname,
				      const Attributes&  attrs )
{
  char * elem = XMLString::transcode(localname);

  if ( XMLString::equals(elem, "person") ) {
    const XMLCh * name = attrs.getValue( _kname );
    curr_person = createPerson( name );
  } else if ( XMLString::equals(elem, "aka") ) {
    proc_aka = true;
  } else if ( XMLString::equals(elem, "actor") ) {
    curr_work = "Actor";
  } else if ( XMLString::equals(elem, "director") ) {
    curr_work = "Director";
  } else if ( XMLString::equals(elem, "producer") ) {
    curr_work = "Producer";
  } else if ( XMLString::equals(elem, "writer" ) ) {
    curr_work = "Writer";
  }

  buffer.clear();
  
  XMLString::release(&elem);  
}

void SAXPeopleHandler::endElement( const XMLCh* const uri, 
				   const XMLCh* const localname,
				   const XMLCh* const qname )
{
  char * elem = XMLString::transcode(localname);

  if ( XMLString::equals(elem, "person") ) {
    curr_person = "";
  } else if ( XMLString::equals(elem, "aka") ) {
    addAka( buffer );
    proc_aka = false;
  } else if ( XMLString::equals(elem, "movie" ) ) {
    addLink(buffer, curr_work);
    /*if( proc_actor ) {
      addLink(buffer, "Actor");
    } else if ( proc_director ) {
      addLink(buffer, "Director");
    } else if ( proc_producer ) {
      addLink(buffer, "Producer");
    } else if ( proc_writer ) {
      addLink(buffer, "Writer");
      }*/
  } else {
    curr_work = "";
    /*    proc_actor = false;
    proc_director = false;
    proc_producer = false;
    proc_writer = false;*/
  }

  XMLString::release(&elem);  
}


void SAXPeopleHandler::characters( const XMLCh* const chars,
				   const XMLSize_t length )
{
  if( proc_aka || curr_work != "" ) 
    buffer.append(chars);
}

void SAXPeopleHandler::fatalError( const SAXParseException& exception )
{
  throw exception;
}

std::string SAXPeopleHandler::createPerson(const XMLCh * name)
{
  std::string pers = "";
  char * name_ch = XMLString::transcode( name );

  pers = db.add_person(name_ch);

  XMLString::release(&name_ch);
  return pers;
}

void SAXPeopleHandler::error( const SAXParseException &exception )
{
  throw exception;
}


void SAXPeopleHandler::addAka(const XercesString& aka)
{
  if( curr_person == "" ) {
    std::cerr << "NO DEURIES HAVER ARRIBAT ACI" << std::endl;
  } else {
    char * name_aka = XMLString::transcode( aka.c_str() );
    db.add_person_aka(name_aka, curr_person);
    XMLString::release(&name_aka);
  }
}

void SAXPeopleHandler::addLink(const XercesString& movie, std::string work)
{
  if ( curr_person == "") {
    std::cerr << "NO DEURIES HAVER ARRIBAT ACI" << std::endl;
  } else {
    char * movie_ch = XMLString::transcode( movie.c_str() );
    std::string movie_id = db.get_movie_id( movie_ch );
    if (movie_id == "") {
      std::cerr << "TITLE NOT FOUND: " << movie_ch << " (IGNORING LINK)" << std::endl;
    } else  
      db.add_link(curr_person, movie_id, work);
    
    XMLString::release(&movie_ch);
  }
}

